package com.gxa.common.utils;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import lombok.extern.slf4j.Slf4j;

/**
 * @author tom
 * @create 2021/11/5 1:38 下午
 * 阿里短信发送Util
 */

public class AliSendSmsUtil {

    private static String accessKeyId = "LTAI5tSiX6kHwQxH3Ak39Qd2";

    private static String accessKeySecret = "mt4yAQR4pDhN5feb1mskcGmzGawLu1";

    private static String signName = "刀锋之影";

    /**
     * 更换手机号验证码模板
     */
    private static String REPLACE_MOBILE_ID = "SMS_199796966";




    public static Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config();
        config.accessKeyId = accessKeyId;
        config.accessKeySecret = accessKeySecret;
        return new Client(config);
    }

    public static boolean sendSms(String mobile, String smsCode) {
        if(mobile!=null&&mobile!=""&&smsCode!=null&&smsCode!=""){
            try {
                Client client = AliSendSmsUtil.createClient(accessKeyId, accessKeySecret);
                SendSmsRequest sendSmsRequest = new SendSmsRequest();
                sendSmsRequest.setPhoneNumbers(mobile);
                sendSmsRequest.setSignName(signName);
                sendSmsRequest.setTemplateCode(REPLACE_MOBILE_ID);

                sendSmsRequest.setTemplateParam("{\"code\":\"" + smsCode + "\"}");
                SendSmsResponse sendSmsResponse = client.sendSms(sendSmsRequest);
                if (null != sendSmsResponse && null != sendSmsResponse.getBody() && sendSmsResponse.getBody().code.equals("OK")) {
                    return true;
                }
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
        return true;
    }
}
